/**
 * Created by chris on 2017/7/4.
 */
import {Injectable} from '@angular/core';
import {AlertController, Loading, LoadingController, ModalController, ToastController} from "ionic-angular";

@Injectable()
export class CommProvider {

    loading: Loading = null;

    constructor(private toastCtrl: ToastController,
                private alertCtrl: AlertController,
                private modalCtrl: ModalController,
                private loadingCtrl: LoadingController) {
    }

    showLoading() {
        this.loading = this.loadingCtrl.create({
            content: '载入中'
        });
        this.loading.present();
    }

    dismissLoading() {
        if (this.loading) {
            this.loading.dismiss();
            this.loading = null;
        }
    }

    showToast(message: string, duration = 2000, position = 'bottom') {
        this.toastCtrl.create()
            .setMessage(message)
            .setDuration(duration)
            .setPosition('bottom')
            .present();
    }

    showConfirmAlert(title: string, message: string = null): Promise<any> {
        return new Promise((resolve, reject) => {
            let confirm = this.alertCtrl.create({
                title: title,
                message: message,
                buttons: [
                    {
                        text: '取消',
                        handler: () => {
                            console.log('Cancel clicked');
                            resolve(false);
                        }
                    },
                    {
                        text: '确定',
                        handler: () => {
                            console.log('OK clicked');
                            resolve(true);
                        }
                    }
                ]
            });
            confirm.present();
        });
    }

    showCustomConfirmAlert(title: string, message: string, leftBtn: string, rightBtn: string): Promise<any> {
        return new Promise((resolve, reject) => {
            let confirm = this.alertCtrl.create({
                title: title,
                message: message,
                buttons: [
                    {
                        text: leftBtn,
                        handler: () => {
                            console.log('Cancel clicked');
                            resolve(1);
                        }
                    },
                    {
                        text: rightBtn,
                        handler: () => {
                            console.log('OK clicked');
                            resolve(2);
                        }
                    }
                ]
            });
            confirm.present();
        });
    }

    showInputAlert(title: string, inputArray: Array<{name: string, placeholder: string, type: string}>): Promise<any> {
        return new Promise((resolve, reject) => {
            let alert = this.alertCtrl.create({
                title: title,
                inputs: inputArray,
                buttons: [
                    {
                        text: '取消',
                        role: 'cancel',
                        handler: data => {
                            console.log('Cancel clicked');
                        }
                    },
                    {
                        text: '发布',
                        handler: data => {
                            resolve(data);
                        }
                    }
                ]
            });
            alert.present();
        });
    }

    showRadioAlert(title: string, radioArray: Array<any>) {
        return new Promise(resolve => {
            let alert = this.alertCtrl.create();
            alert.setTitle(title);

            for (let i = 0; i < radioArray.length; i++) {
                alert.addInput({
                    type: 'radio',
                    label: radioArray[i].title,
                    value: radioArray[i].value,
                    checked: radioArray[i].checked
                });
            }

            alert.addButton('取消');
            alert.addButton({
                text: '确定',
                handler: data => {
                    resolve(data);
                }
            });
            alert.present();
        });
    }

    showDialogModal(pageName: string, options: any = null) {
        return this.modalCtrl.create(pageName, options, {cssClass: 'inset-modal'}).present();
    }
}